package com.cqw.scheduled;

import com.cqw.service.ActivityService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Author: caoqingwen
 * @CreateTime: 2021-05-24 10:34
 * @Description: 定时将已发布活动修改成进行中
 */
@Component
@Slf4j
public class AutoActivityStart {

    @Autowired
    private ActivityService activityService;

    @Autowired
    private RedisTemplate redisTemplate;

    private static final String AUTO_ACTIVITY_START_KEY = "scheduled:key:auto:activity:start:key";


    @Scheduled(cron = "0 0/3 * * * ? ")
    public void autoActivityStart() {
        if (redisTemplate.opsForValue().setIfAbsent(AUTO_ACTIVITY_START_KEY, "1", 1, TimeUnit.MINUTES)){
            log.info("定时将已发布活动修改成进行中开始==================================");
            activityService.autoActivityStart();
            log.info("定时将已发布活动修改成进行中结束==================================");
        }
    }


}
